<template>
  <div class="workflow-instance">
    <!-- 搜索栏 -->
    <el-card class="search-card">
      <el-form :model="searchForm" ref="searchFormRef" inline>
        <el-form-item label="流程名称" prop="name">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入流程名称"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="业务标识" prop="businessKey">
          <el-input
            v-model="searchForm.businessKey"
            placeholder="请输入业务标识"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="发起人" prop="starter">
          <el-input
            v-model="searchForm.starter"
            placeholder="请输入发起人"
            clearable
            style="width: 120px"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="searchForm.status"
            placeholder="请选择状态"
            clearable
            style="width: 120px"
          >
            <el-option label="进行中" value="running" />
            <el-option label="已完成" value="completed" />
            <el-option label="已终止" value="terminated" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        border
        stripe
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="name" label="流程名称" min-width="150" />
        <el-table-column prop="businessKey" label="业务标识" width="120" />
        <el-table-column prop="currentNode" label="当前节点" width="120" />
        <el-table-column prop="starter" label="发起人" width="100" />
        <el-table-column prop="assignee" label="当前处理人" width="100" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusTag(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="开始时间" width="160" />
        <el-table-column prop="endTime" label="结束时间" width="160" />
        <el-table-column prop="duration" label="耗时" width="100" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleView(scope.row)"
            >查看</el-button>
            <el-button
              link
              type="primary"
              @click="handleDiagram(scope.row)"
            >流程图</el-button>
            <el-button
              link
              type="danger"
              @click="handleTerminate(scope.row)"
              v-if="scope.row.status === 'running'"
            >终止</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 流程图对话框 -->
    <el-dialog
      title="流程图"
      v-model="diagramVisible"
      width="800px"
      append-to-body
    >
      <div class="diagram-container">
        <!-- TODO: 集成流程图展示组件 -->
        <div class="diagram-placeholder">流程图展示区域</div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  name: '',
  businessKey: '',
  starter: '',
  status: ''
})

// 表格数据
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)
const tableData = ref([
  {
    name: '贷款审批流程',
    businessKey: 'LOAN_001',
    currentNode: '风控审核',
    starter: '张三',
    assignee: '李四',
    status: 'running',
    startTime: '2024-03-01 10:00:00',
    endTime: '',
    duration: '2天3小时'
  }
])

// 流程图对话框
const diagramVisible = ref(false)

// 查询
const handleSearch = () => {
  loading.value = true
  // TODO: 实现查询逻辑
  setTimeout(() => {
    loading.value = false
  }, 500)
}

// 重置
const handleReset = () => {
  searchForm.name = ''
  searchForm.businessKey = ''
  searchForm.starter = ''
  searchForm.status = ''
  handleSearch()
}

// 查看
const handleView = (row) => {
  // TODO: 查看流程实例详情
  console.log('查看流程实例', row)
}

// 查看流程图
const handleDiagram = (row) => {
  diagramVisible.value = true
  // TODO: 加载流程图
}

// 终止流程
const handleTerminate = (row) => {
  ElMessageBox.confirm('确认要终止该流程吗？', '提示', {
    type: 'warning'
  }).then(() => {
    // TODO: 实现终止逻辑
    ElMessage.success('终止成功')
    handleSearch()
  })
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}

// 获取状态标签类型
const getStatusTag = (status) => {
  const map = {
    running: 'primary',
    completed: 'success',
    terminated: 'danger'
  }
  return map[status]
}

// 获取状态文本
const getStatusText = (status) => {
  const map = {
    running: '进行中',
    completed: '已完成',
    terminated: '已终止'
  }
  return map[status]
}
</script>

<style scoped>
.workflow-instance {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.diagram-container {
  height: 500px;
  background-color: #f5f7fa;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.diagram-placeholder {
  color: #909399;
  font-size: 14px;
}

:deep(.el-card__body) {
  padding: 20px;
}
</style> 